package com.ciwong.epaper.modules.epaper.adapter;

import android.net.Uri;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.EpaperInfo;
import com.ciwong.epaper.util.DisplayUtils;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

import java.util.List;

/**
 * Created by keepon on 2018/4/10.
 */

public class BookDeskAdapter extends BaseQuickAdapter<EpaperInfo, BookDeskAdapter.BookDeskViewHolder> {


    public BookDeskAdapter(int layoutResId, @Nullable List<EpaperInfo> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BookDeskViewHolder helper, EpaperInfo item) {
        helper.bindData(item);
    }


    public static class BookDeskViewHolder extends BaseViewHolder {
        private Uri uri;
        private EpaperInfo mEpaperInfo;
        private ImageView mIvKaoShi, mIvFree;
        private SimpleDraweeView mIvBook;
        private TextView mTvBookName;

        public BookDeskViewHolder(View view) {
            super(view);
        }

        public void bindData(EpaperInfo epaperInfo) {
            mEpaperInfo = epaperInfo;
            mIvKaoShi = getView(R.id.img_kaoshi1);
            mIvBook = getView(R.id.iv_book);
            mIvFree = getView(R.id.img_free1);
            mTvBookName = getView(R.id.tv_book_name);
            if (epaperInfo != null) {
                if (!TextUtils.isEmpty(epaperInfo.getCover())) {
                    uri = Uri.parse(epaperInfo.getCover());
                    String newUri = uri.toString()+"?x-oss-process=image/resize,w_400,m_mfit/quality,Q_60";
                    uri = Uri.parse(newUri);
                    setNetImg(mIvBook, uri, mIvBook.getWidth(), mIvBook.getHeight());
                } else {
                    setLocalDefaultImg(mIvBook);
                }
                mTvBookName.setText(epaperInfo.getProductName());
                mIvKaoShi.setVisibility(epaperInfo.getRequired() == 1 ? View.VISIBLE : View.INVISIBLE);
                mIvFree.setVisibility(epaperInfo.getIsFree() == 1 ? View.VISIBLE : View.INVISIBLE);
            }
        }


        private void setNetImg(SimpleDraweeView simpleDraweeView, Uri uri, int width, int height) {
            try {
                ImageRequest request = null;
                if (uri != null) {
                    request = ImageRequestBuilder.newBuilderWithSource(uri)
                            .setResizeOptions(new ResizeOptions(DisplayUtils.dip2px(simpleDraweeView.getContext(), 300), DisplayUtils.dip2px(simpleDraweeView.getContext(), 420))).build();

                    DraweeController drawweeController = Fresco.newDraweeControllerBuilder().setUri(uri).setImageRequest(request)
                            //列表滑动会变流畅
                            .setOldController(simpleDraweeView.getController())
                            //                    .setAutoPlayAnimations(true)
                            .build();
                    simpleDraweeView.setController(drawweeController);
                }
            } catch (Exception e) {

            } catch (OutOfMemoryError outOfMemoryError) {

            }


        }

        private void setLocalDefaultImg(SimpleDraweeView simpleDraweeView) {
            Uri uri = Uri.parse("res:///" + R.mipmap.item_load_icon);
            DraweeController controller = Fresco.newDraweeControllerBuilder()
                    .setUri(uri)
                    .setTapToRetryEnabled(true)
                    .build();
            simpleDraweeView.setController(controller);
        }
    }
}
